import { createRouter, createWebHistory } from 'vue-router'
import { emitter } from '@/emitter/emitter'
import { useUserStore } from '@/stores'
import { HomeOutline, ListCircleOutline, PersonOutline, CubeOutline, BanOutline } from '@vicons/ionicons5'
import { ClipboardTaskListLtr24Regular, AppsList20Regular } from '@vicons/fluent'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: () => import('@/views/Layout/index.vue'),
      meta: { requiresAuth: true, title: '管理系统', icon: CubeOutline as any },
      children: [
        {
          path: '/home',
          component: () => import('@/views/Home/index.vue'),
          meta: { requiresAuth: true, title: '首页', icon: HomeOutline as any },
          name: 'home'
        },
        {
          path: '/userDetail',
          component: () => import('@/views/User/index.vue'),
          meta: { requiresAuth: true, title: '个人中心', icon: PersonOutline as any },
          name: 'userDetail'
        },
        {
          path: '/userList',
          component: () => import('@/views/User/userList.vue'),
          meta: {
            requiresAuth: true,
            isAdmin: true,
            superAdmin: true,
            title: '用户列表',
            icon: ListCircleOutline as any
          },
          name: 'userList'
        },
        {
          path: '/noPermissions',
          component: () => import('@/views/Error/NoPermissions.vue'),
          meta: { requiresAuth: true, title: '无权限', icon: BanOutline as any },
          name: 'noPermissions'
        },
        {
          path: '/myTopic',
          component: () => import('@/views/Topic/index.vue'),
          meta: { requiresAuth: true, title: '我的帖子', icon: ClipboardTaskListLtr24Regular as any },
          name: 'myTopic'
        },
        {
          path: '/allTopic',
          component: () => import('@/views/Topic/TopicAll.vue'),
          meta: { requiresAuth: true, isAdmin: true, title: '所有帖子', icon: AppsList20Regular as any },
          name: 'allTopic'
        },
        {
          path: '/:pathMatch(.*)*',
          redirect: '/home'
        }
      ],
      redirect() {
        return '/home'
      }
    },
    { path: '/login', component: () => import('@/views/Login/index.vue'), meta: { requiresAuth: false }, name: 'login' }
  ]
})

router.beforeEach((to) => {
  const userStore = useUserStore()
  // 如果路由需要登录权限，且用户未登录，则跳转到登录页
  if (to.meta.requiresAuth && !userStore.isLogin()) {
    return '/login'
  }
  //管理员权限
  if (to.meta.requiresAuth && to.meta.isAdmin && !userStore.isAdmin()) {
    return '/noPermissions'
  }
  //需要超级管理员权限
  if (
    to.meta.requiresAuth &&
    to.meta.superAdmin &&
    userStore.userInfo.username !== 'admin' // 除了admin用户，其他用户都没有权限，因为一开始数据表设计失误，一开始没考虑到超级管理员的问题，所以这里只有admin用户才有超级管理员权限
  ) {
    return '/noPermissions'
  }
  return true
})
//---------------------------事件监听---------------------------
// 注册一个事件监听，当接收到API:UN_AUTH事件时,跳转到登录页
emitter.on('API:UN_AUTH', () => {
  router.push('/login')
})
// 注册一个事件监听，当接收到USER:LOGOUT事件时,跳转到登录页
emitter.on('USER:LOGOUT', () => {
  router.push('/login')
})

export default router
